package com.example.homework8.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.constraintlayout.widget.Group;
import androidx.fragment.app.Fragment;

import com.example.homework8.R;
import com.example.homework8.bean.PlanInfo;
import com.example.homework8.database.PlanDBHelper;

import java.text.SimpleDateFormat;
import java.util.Date;


public class PlanEditFragment extends Fragment  {
    private final static String TAG = "ShoppingCartActivity";
    private TextView tv_total_price;
    private Group gp_content;
    private LinearLayout ll_cart;
    private int mCount; // 购物车中的商品数量
    private PlanDBHelper mGoodsHelper; // 声明一个商品数据库的帮助器对象
    private PlanDBHelper mCartHelper; // 声明一个购物车数据库的帮助器对象
    // 显示购物车图标中的商品数量
    private Group gp_empty;

    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;
    PlanInfo PlanInfo;
    public PlanEditFragment(PlanInfo PlanInfo) {
        this.PlanInfo = PlanInfo;
        // Required empty public constructor
    }
    public static PlanEditFragment newInstance(String param1, String param2) {
        PlanEditFragment fragment = new PlanEditFragment(new PlanInfo());
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_edit_record, container, false);
        Button sure = view.findViewById(R.id.btn_sure);
        EditText name ,info ,cate ;
        name = view.findViewById(R.id.title);
        cate = view.findViewById(R.id.cate);
        info = view.findViewById(R.id.content);
        name.setText(PlanInfo.name);
        cate.setText(PlanInfo.cate);
        info.setText(PlanInfo.info);
        PlanDBHelper planDBHelper=  PlanDBHelper.getInstance(getActivity(), 1);
        planDBHelper.openReadLink();
        sure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(getActivity(),"保存成功",Toast.LENGTH_SHORT).show();
                SimpleDateFormat ft = new SimpleDateFormat ("yyyy-MM-dd hh:mm:ss");
                PlanInfo.createtime =  ft.format(new Date());
                PlanInfo.name = name.getText().toString();
                PlanInfo.cate = cate.getText().toString();
                PlanInfo.info = info.getText().toString();
                planDBHelper.update(PlanInfo);
            }
        });
        return view;
    }


}